package jp.aist.gtrc.plus.scheduler.node;

import java.util.Collection;
import java.util.LinkedList;

import jp.aist.gtrc.plus.scheduler.status.JobStatus;
import jp.aist.gtrc.plus.scheduler.status.NodeStatus;
import jp.aist.gtrc.plus.scheduler.util.Filter;


public class ProfilingNodeAllocator extends Filter<NodeInfo> implements NodeAllocator {

	private final NodeSorter sorter;

	public ProfilingNodeAllocator() {

		this(null);

	}

	public ProfilingNodeAllocator(NodeSorter sorter) {

		addChecker(new IdleNowNodeChecker());
		addChecker(new ProfilingNodeChecker());

		this.sorter = sorter;

	}

	public Collection<NodeInfo> suitable(Collection<NodeInfo> nodes,
			NodeAllocateRequest allocReq){

		if (sorter != null) {
			nodes = sorter.sort(nodes);
		}

                // The instance of EnoughResourceNodeChecker will be removed
                // automatically after doing filter().
                addOneTimeChecker(new EnoughResourceNodeChecker(allocReq));

		// The instance of WholeResourceNodeChecker will be removed
		// automatically after doing filter().
		//addOneTimeChecker(new WholeResourceNodeChecker(allocReq));

		Collection<NodeInfo> allocated = filter(nodes);
		return (allocReq.isAllocatedEnough() ? allocated : null);

	}

}
